import ctypes
import wx
import wx.adv
import tkinter as tk
import win32con
import pywintypes
import win32api
import configparser

k32 = ctypes.windll.LoadLibrary("kernel32.dll")
whnd = k32.GetConsoleWindow()
if whnd != 0:
	ctypes.windll.user32.ShowWindow(whnd, 0)
	ctypes.windll.kernel32.CloseHandle(whnd)

def getIni(inikey, inivaluse):
	config = configparser.ConfigParser()
	config.read("base.ini")
	return config[inikey][inivaluse]

class rootMain():
	def __init__(self):
		self.root = tk.Tk()

	def EnsureTop(self):
		self.root.lift()
		self.root.after(getIni('caeye', 'RefreshRate'), self.EnsureTop)

	def run(self):
		self.root.overrideredirect(True)
		self.root.attributes("-alpha", getIni('caeye', 'alpha')) #亮度
		w = self.root.winfo_screenwidth()
		h = self.root.winfo_screenheight()
		self.root.geometry("%dx%d" %(w,h))
		self.root.attributes("-topmost",True)
		self.root.lift() #置顶层
		self.EnsureTop();
		self.root.wm_attributes("-disabled", True)
		hWindow = pywintypes.HANDLE(int(self.root.frame(), 16))
		exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
		win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)
		canvas = tk.Canvas(self.root)
		canvas.configure(width = w)
		canvas.configure(height = h)
		canvas.configure(bg = getIni('caeye', 'color'))
		canvas.configure(highlightthickness = 0)
		canvas.pack()
		self.root.mainloop()

class FolderBookmarkTaskBarIcon(wx.adv.TaskBarIcon):
	ICON = 'favicon.ico'
	TITLE = '护眼模式'

	MENU_EXIT = wx.NewIdRef(count=1)

	def __init__(self):
		super().__init__()

		# 设置图标和提示
		self.SetIcon(wx.Icon(self.ICON), self.TITLE)

		# 绑定菜单项事件
		self.Bind(wx.EVT_MENU, self.onExit, id=self.MENU_EXIT)

	def CreatePopupMenu(self):
		'''生成菜单'''

		menu = wx.Menu()
		# 添加两个菜单项
		menu.Append(self.MENU_EXIT, '开启/退出')
		return menu

	def onExit(self, event):
		wx.Exit()


class MyFrame(wx.Frame):
	def __init__(self):
		super().__init__()
		FolderBookmarkTaskBarIcon()


class MyApp(wx.App):
	def OnInit(self):
		MyFrame()
		return True


if __name__ == "__main__":
	app = MyApp()
	app.MainLoop()

	root = rootMain()
	root.run()